var f0 = 'f0';
var w0 = 'w0';
var w1 = 'w1';
var w2 = 'w2';
var w3 = 'w2';
var h0 = 'h0';
var h1 = 'h1';
var h2 = 'h2';
var e0 = 'e0';
var e1 = 'e1';
var e2 = 'e2';
var e3 = 'e3';
var u1 = 'u1';
var u2 = 'u2';
var u3 = 'u3';

var blocks = {
    wall:[w0, w1, w2, w3, h0, h1, h2],
    walkable:[f0],
    explodable:[e0, e1, e2, e3],
    upgradable:[u1, u2, u3]
};

/**
 * Initializes variables in main.js file
 */
function mapLoaded() {
    var map = OurGame.mapFiles[OurGame.mapFiles.length - 1];
    if (map) {
        level.name = map.name;
        level.spawnPos = map.spawnPos;
        level.map = rows2cols(map.map);
    }
    OurGame.mapLoaded = true;
    OurGame.booleanMap = mapToBoolean();
}

function mapToBoolean() {
    var booleanMap = [];
    for (var i = 0; i < level.map.length; i++) {
        if (!booleanMap[i]) {
            booleanMap[i] = [];
        }
        for (var j = 0; j < level.map[0].length; j++) {
            if (level.map[i][j].charAt(0) == "w" || level.map[i][j].charAt(0) == "h" || level.map[i][j].charAt(0) == "e") {
                booleanMap[i][j] = 1;
            } else {
                booleanMap[i][j] = 0;
            }
        }
    }
    return booleanMap;
}


/**
 * This is a transpose function. Changes lines to columns and contrary
 *
 * @param map array [[a1, a2, ...], [a1, a2, ...], ...]
 * @return array [[a1, b1, ...], [a2, b2, ...], ...]
 */
function rows2cols(map) {
    var r = [];
    var t;
    for (var i = 0, iLen = map.length; i < iLen; i++) {
        t = map[i];
        for (var j = 0, jLen = t.length; j < jLen; j++) {
            if (!r[j]) {
                r[j] = [];
            }
            r[j][i] = t[j];
        }
    }
    return r;
}

function isExplodable(pos) {
    try {
        return blocks.explodable.indexOf(level.map[pos.x][pos.y].slice(0,2)) >= 0;
    }
    catch (e) {
        return false;
    }
}

function isWall(pos) {
    try {
        return blocks.wall.indexOf(level.map[pos.x][pos.y]) >= 0;
    }
    catch (e) {
        return false;
    }
}

function isWalkable(pos) {
    try {
        return blocks.walkable.indexOf(level.map[pos.x][pos.y]) >= 0;
    }
    catch (e) {
        return false;
    }
}

/**
 * Replaces given blocks with random walkable block or upgrades
 *
 * @param blocksToExplode [{x, y}]
 */
function explodeBlocks(blocksToExplode) {
    var randomInt;
    var randomUpgrade;
    for (var i = 0; i < blocksToExplode.length; i++) {

        randomInt = Math.floor(Math.random() * blocks.walkable.length);
        randomUpgrade = Math.floor(Math.random() * blocks.upgradable.length);
        var matrixObjects = removeObjectFromMatrixElement(blocksToExplode[i]['x'], blocksToExplode[i]['y'], "e");
        var newMatrixElement = createUpdatedMatrixElement(matrixObjects);
        if (newMatrixElement === "") {
            level.map[blocksToExplode[i]['x']][blocksToExplode[i]['y']] = blocks.walkable[randomInt];
        } else {
            level.map[blocksToExplode[i]['x']][blocksToExplode[i]['y']] = newMatrixElement;
        }

    }
    OurGame.mapChanged = true;
}